<template>
  <div>
    <el-dialog
      title="批量导入标签"
      v-model="isDialog"
      destroy-on-close
      width="460px"
      @open="openDialog"
      :before-close="cancelExport"
    >
      <div class="uploadExcel">
        <div class="tips">
          点击下载<span @click="download">Excel表格模板</span>
        </div>
        <div style="display: flex;">
        <span>标签分组：</span>
        <!-- <el-select v-model="categoryId" placeholder="请选择标签分类">
          <el-option
            v-for="item in tagGroupList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select> -->
        <TreeSelect class="_tree_select" v-model="categoryId" :treeSelectData="treeSelectData" :treeProps="{
            label: 'name',
            children: 'children',
            disabled: (data) => {
              return Boolean(data.nodeType && !data.children);
            },
          }" placeholder="请选择分组"></TreeSelect>
          </div>
        <el-upload
          v-if="isDialog"
          class="upload"
          ref="upload"
          drag
          :limit="1"
          action=""
          accept=".xlsx,.xls"
          :show-file-list="true"
          :http-request="upload"
        >
          <div class="uploadSlot">
            <p>点击或文件拖拽到这里上传</p>
            <div class="uploadSlot-tip">
              *仅支持xlsx格式，请将大小控制在20M以内
            </div>
          </div>
        </el-upload>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancelExport()">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import CustomerApi from "@/api/customerApi";
import { fileDownload } from "@/utils"
export default {
  props: {
    isExport: {
      require: false,
      type: Boolean,
      default: () => {
        return false;
      },
    },
  },
  data() {
    return {
      isDialog: false,
      form: {},
      categoryId: "",
      treeSelectData:[],
      // tagGroupList: [],
    };
  },
  watch: {
    isExport(val) {
      this.isDialog = val;
    },
  },
  methods: {
    async download() {
      fileDownload('./fileTemplate/customerTag.xlsx', '标签列表.xlsx')
    },
    upload(e) {
      let file = e.file;
      this.form.file = file;
      // this.$set(this.form, "file", file);
    },
    async getTagGroupTree() {
      let rs = await CustomerApi.getTagGroupTree({ nodeType: "" });
      if (rs.code === 200) {
        this.treeSelectData = rs.payload;
      }
    },
    async submit() {
      if(!this.categoryId) {
        ElMessage.warning('请选择标签分类')
        return
      }
      let formdata = new FormData();
      formdata.append('categoryId', this.categoryId);
      for (const key in this.form) {
        if (Object.hasOwnProperty.call(this.form, key)) {
          const element = this.form[key];
          formdata.append(key, element);
        }
      }
      let rs = await CustomerApi.exportExcel(formdata);
      if (rs.code === 200) {
        ElMessage.success("批量导入成功");
        this.confirmExoprt();
      }
    },
    // async getTagGroupList () {
    //   let params = {
    //     showCount: -1,
    //     currentPage: 1,
    //   }
    //   let rs = await CustomerApi.getTagGroupList(params);
    //   if(rs.code === 200) {
    //     this.tagGroupList = rs.payload
    //   }
    // },
    openDialog() {
      if(this.treeSelectData.length) {
        return
      }
      this.getTagGroupTree()
    },
    cancelExport() {
      this.$refs.upload.clearFiles();
      this.form = {};
      this.$emit("cancelExport");
    },
    confirmExoprt() {
      this.$emit("confirmExoprt");
      this.cancelExport();
    },
  },
};
</script>


<style lang="scss" scoped>
.tips {
  font-size: 12px;
  font-weight: 400;
  color: #999999;
  line-height: 17px;
  margin-bottom: 8px;
  padding-left: 5px;
  span {
    color: #6178ff;
    cursor: pointer;
  }
}
.upload {
  width: 412px;
  margin-top: 20px;
  :deep(.el-upload-dragger) {
    width: 100%;
    border: 0;
    padding: 0;
    border-radius: 2px;
    // border-bottom: 1px solid #dae0ff;
  }
}
.uploadSlot {
  width: 100%;
  height: 85px;
   background: url("./../../../../assets/images/_upload_icon.png") center / 100% no-repeat;
  padding-top: 102px;
  text-align: center;
  p {
    font-size: 16px;
    color: #333333;
    line-height: 24px;
    margin-bottom: 4px;
  }
  &-tip {
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    line-height: 22px;
  }
}
.message-style .el-message-box__content {
  max-height: 450px;
  overflow: hidden;
  overflow-y: scroll;
}
.el-select {
  width: 342px;
}
</style>